﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;

namespace TrackerRealm.Jazz.Client
{
    /// <summary>
    /// 
    /// </summary>
    public static class IEnumerablejObectExtensionMethods
    {
        //
        // IEnumerable<jObject> Extension methods
        //
        #region Contains
        /// <summary>
        /// Returns true if the sequence contains an element with the specified '<paramref name="name"/>'.
        /// </summary>
        /// <param name="members"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool Contains(this IEnumerable<jObject> members, string name)
        {
            return (members.FirstOrDefault(m =>m.Name == name) != null);
        }
        #endregion
        #region FirstOrEmpty
        /// <summary>
        /// Returns the first element of the sequence that satisfies a condition or a
        /// 'Empty' value if no such element is found.
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source"></param>
        /// <param name="nexus"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public static TSource FirstOrEmpty<TSource>(this IEnumerable<TSource> source, ClientNexusBase nexus, Func<TSource, bool> predicate) where TSource: jObject
        {
            TSource s = source.FirstOrDefault(predicate);
            if (s != null) return s;
            if (nexus == null) 
                throw new ArgumentNullException("nexus", "The parameter is not allowed to be null.");
            return nexus.Empty<TSource>();
        }
        #endregion
    }
}
